#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/select.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <string>

const int size = 1024;

class Epoller
{
public:
    Epoller()
    {
        _epfd = epoll_create(size);
        if(_epfd >= 0)
        {
            log(Info,"epoll_create successfully, _epfd is %d", _epfd);
        }
        else
        {
            log(Error,"epoll_create failed.");
        }
        _timeout = -1;
    }
    ~Epoller()
    {
        if(_epfd >= 0)      //记得关闭
        {   
            close(_epfd);
        }
    }

    void EpollerUpdate(int op, int fd, uint32_t events)  //这里传events即可，在函数内部再创建epoll_event对象
    {
        epoll_event ep_event;
        ep_event.events = events;
        ep_event.data.fd = fd;  //? --> 如果不主动设置，在Dispatcher时拿不到正确的结果(32764)！且epoll_ctl会failed!（因为此时的fd也是无效的）
        int ret = epoll_ctl(_epfd, op, fd, &ep_event);
        if(ret < 0)
        {
            log(Error,"epoll_ctl failed.");
        }

    }

    int EpollerWait(epoll_event* ep_events, int maxevents)
    {
        return epoll_wait(_epfd, ep_events, maxevents, _timeout);
    }


private:
    int _epfd;   
    int _timeout;
};



